
const a = 6378245.0;
const ee = 0.00669342162296594323;
const pi = 3.14159265358979324;
const x_pi = 3.14159265358979324 * 3000.0 / 180.0;
const EARTH_RADIUS = 6378137;

/**
 * GCJ02(国家局、高德、谷歌)转换为百度坐标
 */
export function bd_encrypt(x, y) {
  let z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
  let theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
  let xy = [];
  xy[0] = z * Math.cos(theta) + 0.0065;
  xy[1] = z * Math.sin(theta) + 0.006;
  return xy;
}

/**
 * 百度坐标转换为GCJ02(国家局、高德、谷歌)
 */
export function bd_decrypt(x, y) {
  x = x - 0.0065;
  y = y - 0.006;
  let z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
  let theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
  let xy = [];
  xy[0] = z * Math.cos(theta);
  xy[1] = z * Math.sin(theta);
  return xy;
}

/**
 * GCJ02(国家局、高德、谷歌)转换为原始坐标(WGS84)
 */
export function GcjToWgs(x, y) {
  let wgLoc = [];
  let wgX = x,
    wgY = y;
  let dX, dY;
  let currGcLoc = [];
  let maxCount = 100;
  let count = 0;
  while (true) {
    currGcLoc = WgsToGcj(wgX, wgY);
    dX = x - currGcLoc[0];
    dY = y - currGcLoc[1];
    if (Math.abs(dY) < 1e-5 && Math.abs(dX) < 1e-5) { // 1e-6 ~ centimeter level accuracy
      wgLoc[0] = wgX;
      wgLoc[1] = wgY;
      break;
    }
    wgX += dX;
    wgY += dY;
    if (count > maxCount) { //超过100次计算未满足条件，返回0
      wgLoc[0] = 0;
      wgLoc[1] = 0;
      break;
    }
    count++;
  }
  return wgLoc;
}

/**
 * 原始坐标(WGS84)转换为GCJ02(国家局、高德、谷歌)
 */
export function WgsToGcj(x, y) {
  let gcjLoc = [];
  if (outOfChina(x, y)) {
    gcjLoc[0] = x;
    gcjLoc[1] = y;
    return gcjLoc;
  }
  let dLat = transLat(x - 105.0, y - 35.0);
  let dLon = transLon(x - 105.0, y - 35.0);
  let radLat = y / 180.0 * pi;
  let magic = Math.sin(radLat);
  magic = 1 - ee * magic * magic;
  let sqrtMagic = Math.sqrt(magic);
  dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
  dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
  gcjLoc[1] = y + dLat;
  gcjLoc[0] = x + dLon;
  return gcjLoc;
}

export function WgsToBd09(coor){
	let gcj = WgsToGcj(coor[0],coor[1])
	return bd_encrypt(gcj[0], gcj[1]);
}

export function transLat(x, y) {
  let ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(x > 0 ? x : -x);
  ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
  ret += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
  ret += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;
  return ret;
}

export function transLon(x, y) {
  let ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(x > 0 ? x : -x);
  ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
  ret += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;
  ret += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0 * pi)) * 2.0 / 3.0;
  return ret;
}

export function outOfChina(x, y) {
  if (x < 72.004 || x > 137.8347)
    return true;
  if (y < 0.8293 || y > 55.8271)
    return true;
  return false;
}


export function rad(d) {
  return d * Math.PI / 180.0;
}

/**
 * 根据两点间经纬度坐标（double值），计算两点间距离，单位为米
 */
export function getDistance(lng1, lat1, lng2, lat2) {
  let radLat1 = rad(lat1);
  let radLat2 = rad(lat2);
  let a = radLat1 - radLat2;
  let b = rad(lng1) - rad(lng2);
  let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
    Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
  s = s * EARTH_RADIUS;
  s = Math.round(s * 10000) / 10000;
  return s;
}
